﻿# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from ktit.shm.models import Department, Employee


from ktit.storage.managers import PartCountManager

NUMBERED_CHOICES = (
    ('Y', 'Y'),
    ('N', 'N'),
)

DOCUMENT_KIND_CHOICES = (
    ('D', 'Delivery'),
    ('R', 'Receipt'),
)

STATE_CHOICES = (
    ('N', 'New'),
    ('R', 'Register'),
)



class Category(models.Model):
    parent = models.ForeignKey('self', blank=True, null=True, verbose_name="Принадлежность")
    name = models.CharField(max_length=32, verbose_name="Наименование")
    description = models.TextField(blank=True, verbose_name="Описание")
    date = models.DateTimeField(auto_now_add=True, verbose_name="Дата")   
    sort = models.IntegerField(blank=True, default=10, verbose_name="Сортировка")
    #picture = models.ImageField(upload_to='c:/tmp')
    picture = models.CharField(max_length=128, blank=True, verbose_name="Изображение")
    comment = models.CharField(max_length=64, blank=True, verbose_name="Примечание")

    class Meta:
        ordering = ['sort']
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'
    
    def __unicode__(self):
        return self.name
        
        

class Unit(models.Model):
    name = models.CharField(max_length=32, verbose_name="Единицы")
    reduction = models.CharField(max_length=16, verbose_name="Сокращение")
    date = models.DateTimeField(auto_now_add=True, verbose_name="Дата")
    comment = models.CharField(max_length=64, blank=True, verbose_name="Примечание") 

    class Meta:
        ordering = ['name']
    
    def __unicode__(self):
        return self.name 
 
 
 
class Place(models.Model):
    room = models.CharField(max_length=32, blank=True, verbose_name="Помещение")
    rack = models.CharField(max_length=16, blank=True, verbose_name="Стеллаж")
    shelf = models.CharField(max_length=16, blank=True, verbose_name="Полка")
    box = models.CharField(max_length=16, blank=True, verbose_name="Коробка")

    class Meta:
        ordering = ['rack', 'shelf']
    
    def __unicode__(self):
        return "S%s.P%s.B%s" % (self.rack, self.shelf, self.box)
   
   
   
class Document(models.Model):
    kind = models.CharField(max_length=1, choices=DOCUMENT_KIND_CHOICES, verbose_name="Тип")
    name = models.CharField(max_length=32, verbose_name="Наименование")
    date = models.DateTimeField(auto_now_add=True, verbose_name="Дата")
    user = models.ForeignKey(User, verbose_name="Автор")
    state = models.CharField(max_length=1, choices=STATE_CHOICES, verbose_name="Состояние")
    comment = models.CharField(max_length=64, blank=True, verbose_name="Примечание")

    class Meta:
        ordering = ['date']

    def __unicode__(self):
        return self.name         




class Part(models.Model):
    name = models.CharField(max_length=128, verbose_name="Наименование")
    category = models.ForeignKey(Category, verbose_name="Категория")
    description = models.TextField(blank=True, verbose_name="Описание")
    date = models.DateTimeField(auto_now_add=True, verbose_name="Дата")
    code = models.CharField(max_length=64, verbose_name="Код")	
    picture = models.CharField(max_length=64, blank=True, verbose_name="Изображение")
    place = models.ForeignKey(Place, verbose_name="Место")
    comment = models.CharField(max_length=64, blank=True, verbose_name="Примечание") 
    #numbered = models.CharField(max_length=1, choices=NUMBERED_CHOICES, verbose_name="Нумерованный")
    unit = models.ForeignKey(Unit, verbose_name="Единицы")  
    quantity = models.IntegerField(verbose_name="Количество")

    class Meta:
        ordering = ['name']
    
    def __unicode__(self):
        return self.name  


class DocumentItem(models.Model):
#class PartDocument(models.Model):
    document = models.ForeignKey(Document, verbose_name="Документ") 
    part = models.ForeignKey(Part, verbose_name="Позиция")
    quantity = models.IntegerField(verbose_name="Количество")
    inventory = models.CharField(max_length=16, blank=True, verbose_name="Инвентарник")  

    def __unicode__(self):
        return self.part.name    


class PartItem(models.Model):
    part = models.ForeignKey(Part, verbose_name="Позиция")
    document_in = models.ForeignKey(DocumentItem, verbose_name="Документ", related_name = 'rnp_indoc_set')
    document_out = models.ForeignKey(DocumentItem, blank=True, null=True, verbose_name="Документ", related_name = 'rnp_outdoc_set')
    document_state = models.CharField(max_length=1, choices=STATE_CHOICES, verbose_name="Состояние")
    serial = models.CharField(max_length=32, blank=True, verbose_name="Серийник")    
    
    def __unicode__(self):
        return self.serial 




    #quantity = models.IntegerField(verbose_name="Количество")

#class DocumentItemSerial(models.Model):
#class PartDocumentSerial(models.Model):
#    docitem = models.ForeignKey(PartDocument, verbose_name="Документ") 
#    serial = models.CharField(max_length=32, verbose_name="Серийник")

#    def __unicode__(self):
#        return self.serial  
      


class DocumentReceipt(models.Model):
    document = models.OneToOneField(Document, verbose_name="Документ") 
    indocument = models.CharField(max_length=32, verbose_name="Наименование входящего документа")
    
    def __unicode__(self):
        return self.indocument 

     

class DocumentDelivery(models.Model):
    document = models.OneToOneField(Document, verbose_name="Документ")
    department = models.ForeignKey(Department, verbose_name="Отдел")
    room = models.CharField(max_length=32, blank=True, verbose_name="Комната")
    phone = models.CharField(max_length=32, blank=True, verbose_name="Телефон")
    person = models.CharField(max_length=32, verbose_name="Получил")
    employee = models.ForeignKey(Employee, blank=True, verbose_name="Установил", related_name="doc_usermount_set")
    compserial = models.CharField(max_length=32, blank=True, verbose_name="Номер компьютера")
    
    def __unicode__(self):
        return self.name 



  


""" 
class RegPart(models.Model):
    part = models.ForeignKey(Part, verbose_name="Позиция")
    date = models.DateTimeField(verbose_name='Дата')
    doc = models.ForeignKey(Document, verbose_name="Документ")
    doc_kind = models.CharField(max_length=1, choices=DOCUMENT_KIND_CHOICES, verbose_name="Тип")
    quantity = models.IntegerField(verbose_name="Количество")    
    
    partcount = PartCountManager()
    
    def __unicode__(self):
        return self.date  



class RegNumberedPart(models.Model):
    part = models.ForeignKey(Part, verbose_name="Позиция")
    serial = models.CharField(max_length=32, verbose_name="Серийник")
    in_date = models.DateTimeField(verbose_name='Дата прихода')
    in_doc = models.ForeignKey(Document, verbose_name="Документ", related_name = 'rnp_indoc_set')
    out_date = models.DateTimeField(blank=True, null=True, verbose_name="Дата выдачи")
    out_doc = models.ForeignKey(Document, blank=True, null=True, verbose_name="Документ", related_name = 'rnp_outdoc_set')
    
    def __unicode__(self):
        return self.name 

"""


   


